package com.prestamomo.vil.ppa.pmvilmedium.pmviluse;

import android.Manifest;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;

import androidx.core.app.ActivityCompat;

import com.prestamomo.vil.ppa.PmvilApp;

public class PmvilPermissMd {

   public static String[] pmvilPermiss = {
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_CALENDAR, Manifest.permission.READ_PHONE_STATE,
           Manifest.permission.READ_SMS
    };
   public static String[] pmvilPermissCamera = {Manifest.permission.CAMERA};

   private static String pmvilCamerPermiss = Manifest.permission.CAMERA;

    public static void getPmvilPermiss(Activity pmvilAty, String[] pmvilPermisStr) {
        ActivityCompat.requestPermissions(pmvilAty, pmvilPermisStr, 200);
    }

    public static boolean checkPmvilAllPermiss(Context pmvilContext) {
        boolean isPmvilHaveAllPermis = false;
        for (String pmvilStr : pmvilPermiss) {
            isPmvilHaveAllPermis = ActivityCompat.checkSelfPermission(pmvilContext, pmvilStr)== PackageManager.PERMISSION_GRANTED;
            if(!isPmvilHaveAllPermis){
                return false;
            }
        }
        return isPmvilHaveAllPermis;
    }

    public static boolean checkPmlCameraPermiss(Context context){
        return ActivityCompat.checkSelfPermission(context, pmvilCamerPermiss)== PackageManager.PERMISSION_GRANTED;
    }

    public static boolean checkPmvpMiui(){
        String Pmvpbrand = Build.BRAND;
        switch (Pmvpbrand) {
            case "xiaomi":
            case "Xiaomi":
            case "XiaoMi":
                return true;
            default:
                return false;
        }
    }

    public static boolean phonePmvpXMi(Activity PmvpAty){
        if (checkPmvpMiui()){
            if(checkPmvpXMIPer(PmvpAty)){
                return true;
            }
            return pmvpXMiSms(PmvpAty);
        }else {
            return true;
        }
    }

    private static boolean checkPmvpXMIPer(Context pmvpContext) {
        AppOpsManager pmvpService = (AppOpsManager) pmvpContext.getSystemService(Context.APP_OPS_SERVICE);
        int pmvpMode = pmvpService.checkOpNoThrow("android:read_sms", android.os.Process.myUid(), PmvilApp.getPmvilApss().getPackageName());
        return pmvpMode == AppOpsManager.MODE_ALLOWED;
    }

    public static boolean pmvpXMiSms(Context pmvpCext){
        String pmvpSmsMi = (String) PmvilSpMd.getPmvaParam(pmvpCext, "MUI_SMS_MI","");
        if (!pmvpSmsMi.equals("first_one")){
            PmvilSpMd.putPmvaParam(pmvpCext, "MUI_SMS_MI","first_one");
            Uri pmvpUri = Uri.parse("content://sms/");
            String[] pmvpProjection = new String[]{"_id", "address"};
            Cursor pmvpCur = pmvpCext.getContentResolver().query(pmvpUri, pmvpProjection, null,
                    null, "date desc");
            pmvpCur.close();
            return true;
        }
        return false;
    }
}
